package com.swpu.cbg.cnshop.activity;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.swpu.cbg.cnshop.R;
import com.swpu.cbg.cnshop.bean.Tab;
import com.swpu.cbg.cnshop.fragment.CartFragment;
import com.swpu.cbg.cnshop.fragment.CategoryFragment;
import com.swpu.cbg.cnshop.fragment.HomeFragment;
import com.swpu.cbg.cnshop.fragment.HotFragment;
import com.swpu.cbg.cnshop.fragment.MineFragment;
import com.swpu.cbg.cnshop.widget.FragmentTabHost;
import com.swpu.cbg.cnshop.widget.cnToolbar;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements Toolbar.OnMenuItemClickListener, TabHost.OnTabChangeListener {

    private FragmentTabHost mHost;
    private LayoutInflater mInflater;
    private List<Tab> tabs;
    private cnToolbar toolbar;
    private CartFragment mFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();
        initTabHost();
        toolbar = (cnToolbar) findViewById(R.id.toolbar);
        //setSupportActionBar(toolbar);
        initToolbar();
    }

    private void initToolbar() {
//        toolbar.inflateMenu(R.menu.menu_main);
//        toolbar.setOnMenuItemClickListener(this);
    }

    private void initTabHost() {
        mHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mHost.setup(this, getSupportFragmentManager(), R.id.id_main_content);
        initTab();
        for (Tab tab : tabs) {
            TabHost.TabSpec tabSpec = mHost.newTabSpec(getString(tab.getTitle()));
            tabSpec.setIndicator(buildIndcater(tab));
            mHost.addTab(tabSpec, tab.getFragment(), null);
        }
        mHost.setCurrentTab(0);
        mHost.setOnTabChangedListener(this);

    }

    public void hideToolBar(){
        toolbar.setVisibility(View.GONE);
    }

    public void showToolBar(){
        toolbar.setVisibility(View.VISIBLE);
    }

    private void initTab() {
        Tab home = new Tab(R.string.home, R.drawable.selector_icon_home, HomeFragment.class);
        Tab hot = new Tab(R.string.hot, R.drawable.selector_icon_hot, HotFragment.class);
        Tab category = new Tab(R.string.category, R.drawable.selector_icon_category, CategoryFragment.class);
        Tab cart = new Tab(R.string.cart, R.drawable.selector_icon_cart, CartFragment.class);
        Tab mine = new Tab(R.string.mine, R.drawable.selector_icon_mine, MineFragment.class);
        tabs = new ArrayList<>();
        tabs.add(home);
        tabs.add(hot);
        tabs.add(category);
        tabs.add(cart);
        tabs.add(mine);
    }

    private void initData() {
        mInflater = LayoutInflater.from(this);
    }


    private View buildIndcater(Tab tab) {
        View view = mInflater.inflate(R.layout.tab_indicator, null);
        ImageView img = (ImageView) view.findViewById(R.id.icon_tab);
        TextView tv = (TextView) view.findViewById(R.id.txt_indicator);
        img.setImageResource(tab.getResId());
        tv.setText(tab.getTitle());
        return view;
    }

    public void AddTab(String tabName, String tabContent, int resid, Class clazz) {
        TabHost.TabSpec tabSpec = mHost.newTabSpec(tabName);
        View view = mInflater.inflate(R.layout.tab_indicator, null);
        ImageView img = (ImageView) view.findViewById(R.id.icon_tab);
        TextView tv = (TextView) view.findViewById(R.id.txt_indicator);
        img.setImageResource(resid);
        tv.setText(tabContent);
        tabSpec.setIndicator(view);
        mHost.addTab(tabSpec, clazz, null);
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings:
                Toast.makeText(this,"click", Toast.LENGTH_SHORT).show();
                break;
        }
        return false;
    }

    @Override
    public void onTabChanged(String tabId) {
        mHost.setCurrentTabByTag(tabId);
        toolbar.setTitle(tabId);
        if(tabId==getString(R.string.cart)) {
            showToolBar();
            toolbar.showEditButton();
            toolbar.hideRightButton();
            if(mFragment==null){
                Fragment fragment=getSupportFragmentManager().findFragmentByTag(tabId);
                if(fragment!=null) {
                    mFragment= (CartFragment) fragment;
                    mFragment.refreshData();
                }
            }else{
             mFragment.refreshData();
            }
        }  if(tabId==getString(R.string.mine)) {
            hideToolBar();
        }else {
            showToolBar();
            toolbar.hideEditButton();
            toolbar.showRightButton();
        }
    }
}
